<?php
/*******************************************************************************
 * LightPress - http://lightpress.org
 *
 * Copyright 2004-2005 Ludovico Magnocavallo.
 * Released under the GPL http://www.gnu.org/copyleft/gpl.html
 *
 * developers:
 *              Ludovico Magnocavallo <ludo@asiatica.org>
 *              Jerome Lavigne <jerome@vapourtrails.ca>
 *              Luca Lizzeri <luca.lizzeri@gmail.com>
 *
 * $Id: MailHelper.php 168 2005-10-11 16:26:28Z ludo $
 ******************************************************************************/

class MailHelper {
    
    var $mail_from;
    var $mailer;
    var $mailer_path;
    var $_trans;
    
    function MailHelper($mail_from, $mailer, $mailer_path=null) {
        $this->mail_from = $mail_from;
        $this->mailer = $mailer;
        $this->mailer_path = $mailer_path;
        $this->_trans = array_flip(get_html_translation_table(HTML_ENTITIES));
    }
    
    
    function htent2qp($ascii_in = "") {
        // convert a string to quoted-printable
        return str_replace("%", "=", rawurlencode(strtr($ascii_in, $this->_trans)));
    }

    function encoded2hdr($encoded_in = "", $charset = 'UTF-8') {
        // convert a potentially encoded string to internationalized header
        // representation as for RFC2045, suitable for small strings only
        // as it does not wrap the header
        return sprintf('=?%s?Q?%s?=', $charset, $this->htent2qp(strip_tags($encoded_in)));
    }

    function send_mail($to, $subject, $message, $headers=null, $extra_params=null, $mail_from=null) {
        $mail_from = (is_null($mail_from) ? $this->mail_from : $mail_from);
        
        switch ($this->mailer) {
            
            case 'qmail':
                
                $mailer_path = (is_null($this->mailer_path) ? '/var/qmail/bin/qmail-inject' : $this->mailer_path);
                
                if (!$fh = popen("$mailer_path -f" . $mail_from, 'w'))
                    return $this->raiseError("cannot open fh for qmail_inject", "send_mail", __LINE__);

                $mail = "To: $to\n" .
                    "Subject: $subject\n" .
                    (!empty($headers) ? str_replace("\r\n", "\n", trim($headers)) : '') .
                    "\n" .
                    str_replace("\r\n", "\n", $message);
                $bytes = fputs($fh, $mail);
                $ret2 = (pclose($fh) >> 8) & 0xFF;
                if ($ret2 != 0)
                    return $this->raiseError("cannot send mail", "send_mail", __LINE__);
                
                break;
                
            case 'none':
                break;
                
            default:
                // use PHP's mail() function
                $ret = mail($to, $subject, $message, $headers);
                if (!$ret)
                    return $this->raiseError("cannot send mail", "send_mail", __LINE__);
        }
    }
    
    function raiseError($message, $method, $line, $type=E_USER_WARNING) {
        trigger_error(
            sprintf("%s.%s() line %d: %s", get_class($this), $method, $line, $message),
            $type);
    }

}

?>